//
//  RootViewController2.m
//  UIhomework_14
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RootViewController2.h"
#import "activityModel.h"
#import "UIImageView+WebCache.h"
#import "DataBaseHandle.h"
@interface RootViewController2 ()
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, assign) NSInteger a;
@property (nonatomic, retain) activityModel *actModel;
@end

@implementation RootViewController2
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)dealloc{
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.a = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction1:)];
    self.navigationItem.rightBarButtonItem = item1;
    
    
    
    activityModel *model = _modelArr[_index.row];
    self.navigationItem.title = model.title;
    self.actModel = [activityModel activityModel];
    _actModel = model;
    
    
    
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont  systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scroll.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_scroll];
    _scroll.contentSize = CGSizeMake(0, rect.size.height + 330);
    _scroll.bounces = NO;
    _scroll.pagingEnabled = NO;
    _scroll.showsVerticalScrollIndicator = YES;
    _scroll.contentOffset = CGPointMake(0, 0);
    [_scroll release];
    
    UILabel *labelText  = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 350, 15)];
    labelText.text = model.title;
    [_scroll addSubview:labelText];
    [labelText release];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 75, 93, 190)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    [_scroll addSubview:imgView];
    [imgView release];
    
    UIImageView *imgViewTime = [[UIImageView alloc] initWithFrame:CGRectMake(115, 75, 20, 20)];
    imgViewTime.image = [UIImage imageNamed:@"icon_date_blue@2x"];
    [_scroll addSubview:imgViewTime];
    [imgViewTime release];
    
    UILabel *labelTime = [[UILabel alloc] initWithFrame:CGRectMake(140, 75, 200, 20)];
    NSString *str = model.begin_time;
    NSString *str1 = [str substringFromIndex:5];
    NSString *str2 = model.end_time;
    NSString *str3 = [str2 substringFromIndex:5];
    NSString *str4 = [NSString stringWithFormat:@"%@ -- %@", str1, str3];
    labelTime.text = str4;
    [_scroll addSubview:labelTime];
    [labelTime release];
    
    UIImageView *imgViewAddress = [[UIImageView alloc] initWithFrame:CGRectMake(115, 110, 20, 20)];
    imgViewAddress.image = [UIImage imageNamed:@"icon_sponsor_blue@2x"];
    [_scroll addSubview:imgViewAddress];
    [imgViewAddress release];
    
    UILabel *labelAddress = [[UILabel alloc] initWithFrame:CGRectMake(140, 110, 200, 20)];
    labelAddress.text = model.owne;
    [_scroll addSubview:labelAddress];
    [labelAddress release];
    
    UIImageView *imgViewKind = [[UIImageView alloc] initWithFrame:CGRectMake(115, 155, 20, 20)];
    imgViewKind.image = [UIImage imageNamed:@"icon_catalog_blue@2x"];
    [_scroll addSubview:imgViewKind];
    [imgViewKind release];
    
    UILabel *labelKind = [[UILabel alloc] initWithFrame:CGRectMake(140, 155, 200, 20)];
    labelKind.text = [NSString stringWithFormat:@"类型: %@", model.category_name];
    [_scroll addSubview:labelKind];
    [labelKind release];
    
    UIImageView *imgViewDetailAdd = [[UIImageView alloc] initWithFrame:CGRectMake(115, 200, 20, 20)];
    imgViewDetailAdd.image = [UIImage imageNamed:@"icon_spot_blue@2x"];
    [_scroll addSubview:imgViewDetailAdd];
    [imgViewDetailAdd release];
    
    UILabel *labelDetail = [[UILabel alloc] initWithFrame:CGRectMake(140, 200, 200, 60)];
    labelDetail.text = model.address;
    labelDetail.numberOfLines = 2;
    [_scroll addSubview:labelDetail];
    [labelDetail release];
    
    UILabel *labelActivity = [[UILabel alloc] initWithFrame:CGRectMake(20, 285, 100, 30)];
    labelActivity.text = @"活动介绍";
    [_scroll addSubview:labelActivity];
    [labelActivity release];
    
   
    
    UILabel *bigLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 330, self.view.frame.size.width - 40, rect.size.height)];
    NSLog(@"%f", rect.size.height);
    bigLabel.numberOfLines = 130;
    bigLabel.backgroundColor = [UIColor yellowColor];
    bigLabel.font = [UIFont systemFontOfSize:15];
    bigLabel.text = model.content;
    [_scroll addSubview:bigLabel];
    [bigLabel release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)itemAction:(UIBarButtonItem *)itemaction{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)itemAction1:(UIBarButtonItem *)itemaction{
//    if (_a == 0) {
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:action];
//        [self presentViewController:alert animated:YES completion:nil];
//        _a = _a + 1;
//    }else{
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//        [alert addAction:action];
//        [self presentViewController:alert animated:YES completion:nil];
//    }
    
    
    
      //创建表格
    [[DataBaseHandle shareDataBaseHandle] createTable];
    
    //先查询数据，判断数组是否包含当前页的标题
    NSArray *arr = [[DataBaseHandle shareDataBaseHandle] selectTable];
    NSMutableArray *modelArr = [NSMutableArray array];
    for (TitleModel *model in arr) {
        NSString *str = model.title;
        [modelArr addObject:str];
     }
    
    if (![modelArr containsObject:self.navigationItem.title]) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
        
        
    //不包含的话，插入数据
        TitleModel *model = [TitleModel titleModel];
        model.title = self.navigationItem.title;
        [[DataBaseHandle shareDataBaseHandle] insertModel:model];
    }else{
        //包含的话，直接显示已经收藏
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }
   
    

    }
    
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
